import { _decorator, Color, Component, director, math, Node, Sprite, SpriteFrame, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('heart')
export class heart extends Component {
    value:number;
    start() 
    {
        this.show();
    }
 
    show()
    {
        const sprite = this.node.getComponent(Sprite)
        this.value = Math.floor(Math.random() * (170 - 70) + 70)
        const color = new Color();
        const alpha = {a: 0}
        tween(alpha)
            .to(1,{a:250},{onUpdate:(target:{a:number}, ratio)=>
            {
                color.set(255,this.value,this.value,target.a)
                sprite.color = color;
            },onComplete:(target)=>
            {
                setTimeout(()=>
                {
                    this.action();
                },500)  
            },})
            .start()
    }
    action()
    {
        const sprite = this.node.getComponent(Sprite)
        const color = new Color();
        const alpha = {a: 250}
        tween(alpha)
            .to(1,{a:0},{onUpdate:(target:{a:number}, ratio)=>
            {
                color.set(255,this.value,this.value,target.a)
                sprite.color = color;
            }})
            .to(1,{a:250},{onUpdate:(target:{a:number}, ratio)=>
            {
                color.set(255,this.value,this.value,target.a)
                sprite.color = color;
            }})
            .union()
            .repeatForever()
            .start()
    }

    update(deltaTime: number) 
    {
       
    }
} 
